<?php
/**
 * 毛文科
 */
namespace app\admin\model;
use think\facade\Db;
use think\Request;

class MyAuth
{
    static public function del(){
       $jur = Db::name(getTable('jurs'))
           ->where('status',1)
           ->order('id asc')
           ->select()->toArray();
       $newrole = [];
       foreach ($jur as $value){
           $newrole[] = $value['id'];
       }
       $user = Db::name(getTable('roles'))
           ->select()->toArray();

       foreach ($user as $key=>$value){
           $z = [];
           $role = explode(',',$value['role']);
           foreach ($role as $v){
               if(in_array($v,$newrole)){
                   $z[] = $v;
               }
           }
           $value['role'] = implode(',',$z);
           Db::name(getTable('roles'))
               ->where('id',$value['id'])
               ->update([
                   'role'=>$value['role']
               ]);
           $user[$key] = $value;

       }
       return true;
    }
    public static function add(){
        $jur = Db::name(getTable('jurs'))
            ->where('status',1)
            ->order('id asc')
            ->select()->toArray();
        $role = [];
        foreach ($jur as $value){
            $role[] = $value['id'];
        }
        $role = implode(',',$role);
        return Db::name(getTable('roles'))->where('id',1)->update(['role'=>$role]);
    }
    static public function delTableToLianBiao($id){
        $model = Db::name(getTable('models'))
            ->where('id','in',$id)
            ->select()->toArray();
        $data = [];
        foreach ($model as $value){
            if($value['type']=='联表'){
                $data[] = $value['id'];
            }
        }
        if($data){
            if(Db::name(getTable('jurs'))
                ->where('model_id','in',$data)
                ->delete()){
                MyAuth::del();
                return true;
            }else{
                return false;
            }
        }else{
            return true;
        }
    }
    static public function AuthYanZhen(Request $request){
        $quth = config('auth.');
        $model_id = '';
        $cid = '';
        $cid = $request->param('cid',null);
        if(!$cid){
            $model_id = $request->param('model_id',null);
        }

        $user = session('adminuser');
        if($request->controller()=='Index'){
            return true;
        }
        if(!$user){
            return false;
        }
        $role = Db::name(getTable('roles'))
            ->where('id',$user['role_id'])
            ->find();
        if(!$role){
            return false;
        }
        $array = explode(',',$role['role']);
        foreach ($quth as $key=>$value){
            if($key==$request->controller()){
                foreach ($value as $k=>$v){
                    $ks = explode(',',$k);
                    if(in_array($request->action(),$ks)){
                        $where = [];
                        if($value['cid']??null){
                            $where = [
                                'cid'=>$value['cid']
                            ];
                        }else{
                            if($value['model_id']??null){
                                $where = [
                                    'model_id'=>$value['model_id']
                                ];
                            }
                        }
                        if(!$where){
                            if($cid){
                                $where = [
                                    'cid'=>$cid
                                ];
                            }
                            if($model_id){
                                $where=[
                                    'model_id'=>$model_id
                                ];
                            }
                        }
                        $quanxian = Db::name(getTable('jurs'));
                        foreach ($where as $ka=>$va){
                            $quanxian = $quanxian->where($ka,$va);
                        }
//                        $quanxian = $quanxian->select();
                        $quanxian = $quanxian->where('descs',$v)
                            ->find();

                        if(!in_array($quanxian['id'],$array)){
                            return false;
                        }
                        break;
                    }
                }
            }
        }
        return true;
    }

    /**检测是否有权限
     * @param $requery
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAuthos($requery){
        $model_id = '';
        $cid = '';
        $cid = $requery['cid']??null;
        if(!$cid){
            $model_id = $requery['model_id']??null;
            if(!$model_id){
                return false;
            }
        }

        $user = session('adminuser');
        if(!$user){
            return false;
        }
        $role = Db::name(getTable('roles'))
            ->where('id',$user['role_id'])
            ->find();
        if(!$role){
            return false;
        }
        $array = explode(',',$role['role']);
        if($cid){
            $arr = Db::name(getTable('jurs'))
                ->where('cid',$cid)
                ->field('id')
                ->select()->toArray();
            $false =false;
            foreach ($arr as $value){
                if(in_array($value['id'],$array)){
                    $false = true;
                    break;
                }
            }
            if($false){
                return true;
            }
        }
        if($model_id){
            $arr = Db::name(getTable('jurs'))
                ->where('model_id',$model_id)
                ->field('id')
                ->select()->toArray();
            foreach ($arr as $value){
                if(in_array($value['id'],$array)){
                   return true;
                }
            }
        }
        return false;
    }
}